﻿using Asp.Versioning;
using FlyingEye.ControllerServices;
using FlyingEye.Exceptions;
using FlyingEye.Monitors.Models;
using FlyingEye.Monitors.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace FlyingEye.Controllers
{
    [Authorize]
    [ControllerName("ReadStringValueTask")]
    [ApiVersion("1.0")]
    [Route("api/device-monitor-service/v{version:apiVersion}/read-string-value-tasks")]
    public class ReadStringValueTaskController : DefaultController
    {
        private readonly IReadStringValueTaskRepository _readStringValueTaskRepository;

        private readonly IStringValueMonitorClientRepository _stringValueClientMonitorPointRepository;

        private readonly IStringValueMonitorGroupRepository _stringValueGroupMonitorPointRepository;

        public ReadStringValueTaskController(
            IReadStringValueTaskRepository readStringValueTaskRepository,
            IStringValueMonitorClientRepository stringValueClientMonitorPointRepository,
            IStringValueMonitorGroupRepository stringValueGroupMonitorPointRepository)
        {
            _readStringValueTaskRepository = readStringValueTaskRepository;
            _stringValueClientMonitorPointRepository = stringValueClientMonitorPointRepository;
            _stringValueGroupMonitorPointRepository = stringValueGroupMonitorPointRepository;
        }

        [HttpGet("{id}/string-value-monitor-clients")]
        public async Task<ActionResult<StringValueMonitorClientResponse>> GetStringValueMonitorClientsAsync(Guid id)
        {
            return await this.GetInvokeAsync(async () =>
            {
                var list = await _stringValueClientMonitorPointRepository.GetListAsync(item => item.ReadStringValueTaskId == id);
                return ObjectMapper.Map<List<StringValueMonitorClientModel>, List<StringValueMonitorClientResponse>>(list);
            });
        }

        [HttpGet("{id}/string-value-monitor-groups")]
        public async Task<ActionResult<StringValueMonitorGroupResponse>> GetStringValueMonitorGroupsAsync(Guid id)
        {
            return await this.GetInvokeAsync(async () =>
            {
                var list = await _stringValueGroupMonitorPointRepository.GetListAsync(item => item.ReadStringValueTaskId == id);
                return ObjectMapper.Map<List<StringValueMonitorGroupModel>, List<StringValueMonitorGroupResponse>>(list);
            });
        }

        [HttpGet("{id}", Name = "GetByReadStringValueTaskId")]
        public async Task<ActionResult<ReadStringValueTaskResponse>> GetAsync(Guid id)
        {
            return await this.GetInvokeAsync(async () =>
            {
                var model = await _readStringValueTaskRepository.GetAsync(id);

                return ObjectMapper.Map<ReadStringValueTaskModel, ReadStringValueTaskResponse>(model);
            });
        }

        [HttpGet]
        public async Task<ActionResult<List<ReadStringValueTaskResponse>>> GetListAsync()
        {
            return await this.GetInvokeAsync(async () =>
            {
                var list = await _readStringValueTaskRepository.GetListAsync();
                return ObjectMapper.Map<List<ReadStringValueTaskModel>, List<ReadStringValueTaskResponse>>(list);
            });
        }

        [HttpPut]
        public async Task<ActionResult<ReadStringValueTaskResponse>> UpdateAsync([FromBody] UpdateReadStringValueTaskRequest request)
        {
            if (request.Id == Guid.Empty)
            {
                throw new AppArgumentValidationException("ID 不能为空");
            }

            if (request.DeviceClientId == Guid.Empty)
            {
                throw new AppArgumentValidationException("设备客户端 ID 不能为空");
            }

            if (string.IsNullOrWhiteSpace(request.TaskName))
            {
                throw new AppArgumentValidationException("任务名称不能为空");
            }

            if (string.IsNullOrWhiteSpace(request.DeviceName))
            {
                throw new AppArgumentValidationException("设备名称不能为空");
            }

            if (string.IsNullOrWhiteSpace(request.Address))
            {
                throw new AppArgumentValidationException("地址不能为空");
            }

            if (request.Length <= 0)
            {
                throw new AppArgumentValidationException("字符串长度必须大于 0");
            }

            if (request.Interval < 100)
            {
                throw new AppArgumentValidationException("任务周期必须大于 100");
            }

            return await this.PutInvokeAsync(async () =>
            {
                var model = ObjectMapper.Map<UpdateReadStringValueTaskRequest, ReadStringValueTaskModel>(request);
                var result = await _readStringValueTaskRepository.UpdateAsync(model);
                return ObjectMapper.Map<ReadStringValueTaskModel, ReadStringValueTaskResponse>(result);
            });
        }

        [HttpPost]
        public async Task<ActionResult<ReadStringValueTaskResponse>> InsertAsync([FromBody] CreateReadStringValueTaskRequest request)
        {
            if (request.DeviceClientId == Guid.Empty)
            {
                throw new AppArgumentValidationException("设备客户端 ID 不能为空");
            }

            if (string.IsNullOrWhiteSpace(request.TaskName))
            {
                throw new AppArgumentValidationException("任务名称不能为空");
            }

            if (string.IsNullOrWhiteSpace(request.DeviceName))
            {
                throw new AppArgumentValidationException("设备名称不能为空");
            }

            if (string.IsNullOrWhiteSpace(request.Address))
            {
                throw new AppArgumentValidationException("地址不能为空");
            }

            if (request.Length <= 0)
            {
                throw new AppArgumentValidationException("字符串长度必须大于 0");
            }

            if (request.Interval < 100)
            {
                throw new AppArgumentValidationException("任务周期必须大于 100");
            }

            return await this.InsertInvokeAsync(async () =>
            {
                var model = ObjectMapper.Map<CreateReadStringValueTaskRequest, ReadStringValueTaskModel>(request);
                var result = await _readStringValueTaskRepository.InsertAsync(model);
                return ObjectMapper.Map<ReadStringValueTaskModel, ReadStringValueTaskResponse>(result);
            }, "GetByReadStringValueTaskId", result => new { id = result.Id });
        }

        [HttpDelete("{id}")]
        public async Task<NoContentResult> DeleteAsync(Guid id)
        {
            return await this.DeleteInvokeAsync(async () =>
            {
                await _readStringValueTaskRepository.DeleteAsync(id);
            });
        }
    }
}
